package com.kb.design.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kb.design.common.MeException;
import com.kb.design.constant.Constants;
import com.kb.design.dto.DesignProjectDTO;
import com.kb.design.entity.*;
import com.kb.design.enums.DesignStatus;
import com.kb.design.enums.ProjectStatus;
import com.kb.design.mapper.postgreSQL.*;
import com.kb.design.service.*;
import com.kb.design.util.DataStructureUtil;
import com.kb.design.vo.ProjectNodeInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (CustomUser)表服务实现类
 *
 * @author makejava
 * @since 2023-08-22 10:57:17
 */
@Service
@DS("slave-1")
public class ResponsiblePersonServiceImpl extends ServiceImpl<DesignResponsiblePersonMapper, DesignResponsiblePerson> implements ResponsiblePersonService {

    @Resource
    private DesignResponsiblePersonMapper responsiblePersonMapper;
    @Resource
    private CustomUserService userService;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private CustomUserMapper userMapper;
    @Resource
    private CustomContactMapper customContactMapper;
    @Resource
    private DesignTalkMapper designTalkMapper;
    @Resource
    private DesignDeptMapper designDeptMapper;
    @Resource
    private DesignRoleService designRoleService;
    @Resource
    private CheckValueMapper checkValueMapper;

    @Override
    public Boolean nextTask(DesignResponsiblePerson designResponsiblePerson) {
        //查询projectId最新一条数据 更新完成时间为当前时间
        DesignResponsiblePerson person = responsiblePersonMapper.selectOne(Wrappers.lambdaQuery(DesignResponsiblePerson.class)
                .eq(DesignResponsiblePerson::getProjectId, designResponsiblePerson.getProjectId())
                .orderByDesc(DesignResponsiblePerson::getUpdateTime)
                .last("limit 1"));
        if (person != null) {
            person.setCompleteTime(LocalDateTime.now());
            person.setIsComplete(Constants.YES_COMPLETE);
            responsiblePersonMapper.updateById(person);
        }
        //更新项目相关人字段
        DesignProject project = new DesignProject();
        project.setId(designResponsiblePerson.getProjectId());
        if (designResponsiblePerson.getResponsiblePersonId() != null) {
            //查询项目数据
            DesignProject oldProject = projectMapper.selectById(designResponsiblePerson.getProjectId());
            List<Integer> relevantResponsiblePerson = oldProject.getRelevantResponsiblePerson();
            relevantResponsiblePerson.add(Integer.valueOf(designResponsiblePerson.getResponsiblePersonId()));
            project.setRelevantResponsiblePerson(relevantResponsiblePerson);

        }
        //如果是点击完成 需要更新项目节点
        if (Objects.equals(designResponsiblePerson.getType(), "1")) {
            project.setStatus(Objects.requireNonNull(DesignStatus.getNextStatusByIndex(Integer.parseInt(designResponsiblePerson.getStatus()))).getIndex());
            //如果是设计节点完成 到了检查节点 需要把设计状态值质空 并 插入 检查待确认字段
            //封装完成  设置设计的区域
            project.setDesignDepartments(designResponsiblePerson.getDesignDepartments());
//            }
        } else {
            //判断是否是分配任务 更新对应的负责人
            if (Objects.equals(designResponsiblePerson.getStatus(), DesignStatus.ENCAPSULATION.getIndex())) {
                project.setPageLeader(designResponsiblePerson.getResponsiblePersonId());
            } else {
                project.setDesignLeader(designResponsiblePerson.getResponsiblePersonId());
            }
        }
        projectMapper.updateById(project);
        //插入新的数据
        String nickName = userService.queryNickName(designResponsiblePerson.getResponsiblePersonId());
        designResponsiblePerson.setResponsiblePersonName(nickName);
        responsiblePersonMapper.insert(designResponsiblePerson);
        return true;
    }

    @Override
    @DSTransactional
    public Boolean projectConfirmed(DesignProjectDTO projectDTO) {
        if (StringUtils.isNotBlank(projectDTO.getResponsiblePersonId())) {
            //校验当前人是否有工程师角色
            String createBy = projectDTO.getResponsiblePersonId();
            DesignRole designRole = designRoleService.queryRoleByUserCode(createBy);
            if (designRole.getRoleKey() != null && !designRole.getRoleKey().contains("Engineers")) {
                throw new MeException("当前登录人没有工程师角色，联系管理员添加，再进行分配！");
            }
            //判断分配人是不是主管   是, 替换部门, 不是 走正常分配 无需分配也不走
            if (designRole.getRoleKey() != null && designRole.getRoleKey().contains("Competent") && !Objects.equals(projectDTO.getResponsiblePersonId(), projectDTO.getCreateBy()) &&
                    !Objects.equals(projectDTO.getIsPage(), "0")) {
                replacement(projectDTO);
            } else {
                //判断是否是直接跳过封装
                if (Objects.nonNull(projectDTO.getIsPage()) && projectDTO.getIsPage().equals("0")) {
                    //重置项目关于封装的字段数据  封装部门置空  项目关联人置空
                    projectMapper.update(new DesignProject(), new UpdateWrapper<DesignProject>().lambda().eq(DesignProject::getId, projectDTO.getProjectId())
                            .set(DesignProject::getPageDepartments, null).set(DesignProject::getRelevantResponsiblePerson, null));
                    //全部流程数据软删除
                    responsiblePersonMapper.update(new DesignResponsiblePerson(), new UpdateWrapper<DesignResponsiblePerson>().lambda().eq(DesignResponsiblePerson::getProjectId, projectDTO.getProjectId())
                            .set(DesignResponsiblePerson::getDel, "1"));
                    return this.projectCompleted(projectDTO);
                }

                //查询projectId最新一条数据 更新完成时间为当前时间
                DesignResponsiblePerson person = responsiblePersonMapper.selectOne(Wrappers.lambdaQuery(DesignResponsiblePerson.class)
                        .eq(DesignResponsiblePerson::getProjectId, projectDTO.getProjectId())
                        .eq(DesignResponsiblePerson::getDel, "0")
                        .orderByDesc(DesignResponsiblePerson::getUpdateTime)
                        .last("limit 1"));
                if (person != null) {
                    person.setCompleteTime(LocalDateTime.now());
                    person.setIsComplete(Constants.YES_COMPLETE);
                    responsiblePersonMapper.updateById(person);
                }
                //更新项目相关人字段
                DesignProject project = new DesignProject();
                project.setId(projectDTO.getProjectId());
                if (projectDTO.getResponsiblePersonId() != null) {
                    //查询项目数据
                    DesignProject oldProject = projectMapper.selectById(projectDTO.getProjectId());
                    List<Integer> relevantResponsiblePerson = oldProject.getRelevantResponsiblePerson();

                    //需要根据状态和leader字段进行判断 项目相关人是新增还是替换
                    if (Objects.equals(projectDTO.getStatus(), DesignStatus.ENCAPSULATION.getIndex())) {
                        //如果是封装节点修改  且 pageLeader为空 则添加
                        if (oldProject.getPageLeader() == null) {
                            relevantResponsiblePerson.add(Integer.valueOf(projectDTO.getResponsiblePersonId()));
                        } else {
                            //relevantResponsiblePerson 最新一个项目相关人 替换
                            relevantResponsiblePerson.set(relevantResponsiblePerson.size() - 1, Integer.valueOf(projectDTO.getResponsiblePersonId()));
                            //删除最新的流程明细
                            person.setDel("1");
                            responsiblePersonMapper.updateById(person);
                        }
                    }
                    if (Objects.equals(projectDTO.getStatus(), DesignStatus.DESIGN.getIndex())) {
                        //如果是设计点修改  且 pageLeader为空 则添加
                        if (oldProject.getDesignLeader() == null) {
                            relevantResponsiblePerson.add(Integer.valueOf(projectDTO.getResponsiblePersonId()));
                        } else {
                            //relevantResponsiblePerson 最新一个项目相关人 替换
                            relevantResponsiblePerson.set(relevantResponsiblePerson.size() - 1, Integer.valueOf(projectDTO.getResponsiblePersonId()));
                            //删除最新的流程明细
                            person.setDel("1");
                            responsiblePersonMapper.updateById(person);
                        }
                    }


                    project.setRelevantResponsiblePerson(relevantResponsiblePerson);
                }
                //判断状态分配任务 更新对应的负责人
                if (Objects.equals(projectDTO.getStatus(), DesignStatus.ENCAPSULATION.getIndex())) {
                    project.setPageLeader(projectDTO.getResponsiblePersonId());
                    project.setPageDepartments(projectDTO.getDesignDepartments());
                } else {
                    project.setDesignLeader(projectDTO.getResponsiblePersonId());
                    project.setDesignDepartments(projectDTO.getDesignDepartments());
                }
                String nickName = userService.queryNickName(projectDTO.getResponsiblePersonId());
                project.setResponsiblePersonId(projectDTO.getResponsiblePersonId());
                project.setResponsiblePersonName(nickName);
                project.setProjectStatusAuxiliary(ProjectStatus.IN_PROGRESS.getIndex());

                projectMapper.updateById(project);
                //插入新的数据
                DesignResponsiblePerson designResponsiblePerson = new DesignResponsiblePerson();
                BeanUtil.copyProperties(projectDTO, designResponsiblePerson);
                designResponsiblePerson.setResponsiblePersonName(nickName);
                designResponsiblePerson.setPersonType(Constants.PERSON_TYPE_ENGINEERS);
                designResponsiblePerson.setNodeStatus(designResponsiblePerson.getStatus());

                designResponsiblePerson.setDeptId(projectDTO.getDesignDepartments());
                DesignDept designDept = designDeptMapper.selectById(projectDTO.getDesignDepartments());
                designResponsiblePerson.setDeptName(designDept.getDeptName());
                responsiblePersonMapper.insert(designResponsiblePerson);
            }
        }
        //修改项目基本信息
        this.updateProjectInfo(projectDTO);
        return true;
    }

    public void replacement(DesignProjectDTO projectDTO) {
        DesignProject oldProject = projectMapper.selectById(projectDTO.getProjectId());
        List<Integer> relevantResponsiblePerson = oldProject.getRelevantResponsiblePerson();
        //查询流转信息
        LambdaQueryWrapper<DesignResponsiblePerson> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DesignResponsiblePerson::getProjectId, projectDTO.getProjectId());
        List<DesignResponsiblePerson> person = responsiblePersonMapper.selectList(queryWrapper);

        DesignProject designProject = new DesignProject();
        designProject.setId(projectDTO.getProjectId());
        designProject.setResponsiblePersonId(projectDTO.getResponsiblePersonId());
        String nickName = userService.queryNickName(projectDTO.getResponsiblePersonId());
        designProject.setResponsiblePersonName(nickName);
        //封装节点
        List<Integer> integerList;
        if (Objects.equals(projectDTO.getStatus(), DesignStatus.ENCAPSULATION.getIndex())) {
            designProject.setPageDepartments(projectDTO.getDesignDepartments());
            designProject.setPageLeader(null);
            integerList = person.stream()
                    .filter(item -> Objects.equals(item.getNodeStatus(), DesignStatus.ENCAPSULATION.getIndex()))
                    .map(item -> Integer.parseInt(item.getResponsiblePersonId()))
                    .collect(Collectors.toList());


        } else {
            //设计节点
            designProject.setDesignLeader(null);
            designProject.setDesignDepartments(projectDTO.getDesignDepartments());
            integerList = person.stream()
                    .filter(item -> Objects.equals(item.getNodeStatus(), DesignStatus.ENCAPSULATION.getIndex()))
                    .map(item -> Integer.parseInt(item.getResponsiblePersonId()))
                    .collect(Collectors.toList());
        }
        // 从 relevantResponsiblePerson 中移除 integerList 中的所有元素
        relevantResponsiblePerson.removeAll(integerList);
        relevantResponsiblePerson.add(Integer.valueOf(projectDTO.getResponsiblePersonId()));
        designProject.setRelevantResponsiblePerson(relevantResponsiblePerson);
        //更新项目信息
        projectMapper.replacementUpdate(designProject);
        //更新流转信息
        responsiblePersonMapper.updateByStatus(projectDTO.getProjectId(), projectDTO.getStatus(), "1");
        //插入流转信息
        DesignResponsiblePerson designResponsiblePerson = new DesignResponsiblePerson();
        BeanUtil.copyProperties(projectDTO, designResponsiblePerson);
        designResponsiblePerson.setResponsiblePersonName(nickName);
        designResponsiblePerson.setPersonType(Constants.PERSON_TYPE_COMPETENT);
        designResponsiblePerson.setNodeStatus(designResponsiblePerson.getStatus());

        designResponsiblePerson.setDeptId(projectDTO.getDesignDepartments());
        DesignDept designDept = designDeptMapper.selectById(projectDTO.getDesignDepartments());
        designResponsiblePerson.setDeptName(designDept.getDeptName());
        responsiblePersonMapper.insert(designResponsiblePerson);
    }

    @Override
    public Boolean updateProjectInfo(DesignProjectDTO projectDTO) {
        DesignProject designProject = new DesignProject();
        BeanUtils.copyProperties(projectDTO, designProject, "responsiblePersonId");
        designProject.setId(projectDTO.getProjectId());
        designProject.setUpdateTime(LocalDateTime.now());
        designProject.setUpdateBy(projectDTO.getUpdateBy());
        if (projectDTO.getLayers() != null) {
            designProject.setLayers(String.valueOf(projectDTO.getLayers()));
        }
        projectMapper.updateById(designProject);
        //客户联系信息
        CustomContact customContact = new CustomContact();
        BeanUtil.copyProperties(projectDTO, customContact);
        //先查询 有更新 无插入
        LambdaQueryWrapper<CustomContact> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CustomContact::getProjectId, projectDTO.getProjectId());
        List<CustomContact> customContacts = customContactMapper.selectList(queryWrapper);
        if (customContacts.isEmpty()){
            customContactMapper.insert(customContact);
        }else {
            customContactMapper.update(customContact, Wrappers.lambdaQuery(CustomContact.class).eq(CustomContact::getProjectId, projectDTO.getProjectId()));
        }
        return true;
    }

    @Override
    public Map<String, Object> queryHeadInfo(String id) {
        Map<String, Object> newHashMap = new HashMap<>();
        List<Map<String, Object>> list = DataStructureUtil.buildProjectPhaseList();
        newHashMap.put("steps", list);
        DesignProject designProject = projectMapper.selectById(Long.valueOf(id));
        if (Objects.equals(designProject.getStatus(), DesignStatus.Leader_BONUS_DISTRIBUTION.getIndex())) {
            designProject.setStatus(DesignStatus.BONUS_DISTRIBUTION.getIndex());
        }
        if (Objects.equals(designProject.getStatus(), DesignStatus.WAITING_BONUS_CONFIRMATION.getIndex())) {
            designProject.setStatus(DesignStatus.BONUS_CONFIRMATION.getIndex());
        }
        newHashMap.put("value", designProject.getStatus());
        return newHashMap;
    }

    @Override
    public ProjectNodeInfoVO getProjectNodeInfo(DesignProjectDTO projectDTO) {
        LambdaQueryWrapper<DesignResponsiblePerson> wrapper = Wrappers.lambdaQuery(DesignResponsiblePerson.class).eq(DesignResponsiblePerson::getProjectId, projectDTO.getProjectId())
                .eq(DesignResponsiblePerson::getDel, "0");
        List<DesignResponsiblePerson> responsiblePeople = responsiblePersonMapper.selectList(wrapper);
        ProjectNodeInfoVO projectNodeInfo = new ProjectNodeInfoVO();
        if (CollectionUtil.isNotEmpty(responsiblePeople)) {
            for (DesignResponsiblePerson responsiblePerson : responsiblePeople) {
                if (Objects.equals(responsiblePerson.getNodeStatus(), DesignStatus.ENCAPSULATION.getIndex()) && Objects.equals(responsiblePerson.getPersonType(), Constants.PERSON_TYPE_COMPETENT)) {
                    projectNodeInfo.setPageCompetent(responsiblePerson.getResponsiblePersonName());
                    projectNodeInfo.setPageCompetentId(responsiblePerson.getResponsiblePersonId());
                }
                if (Objects.equals(responsiblePerson.getNodeStatus(), DesignStatus.ENCAPSULATION.getIndex()) && Objects.equals(responsiblePerson.getPersonType(), Constants.PERSON_TYPE_ENGINEERS)) {
                    projectNodeInfo.setPageEngineers(responsiblePerson.getResponsiblePersonName());
                    projectNodeInfo.setPageEngineersId(responsiblePerson.getResponsiblePersonId());
                    projectNodeInfo.setPageCompletionTime(responsiblePerson.getCompleteTime());
                    projectNodeInfo.setPageCompletionTime(responsiblePerson.getCompleteTime());
                }
                if (Objects.equals(responsiblePerson.getNodeStatus(), DesignStatus.DESIGN.getIndex()) && Objects.equals(responsiblePerson.getPersonType(), Constants.PERSON_TYPE_COMPETENT)) {
                    projectNodeInfo.setDesignCompetent(responsiblePerson.getResponsiblePersonName());
                    projectNodeInfo.setDesignCompetentId(responsiblePerson.getResponsiblePersonId());
                }

                if (Objects.equals(responsiblePerson.getNodeStatus(), DesignStatus.DESIGN.getIndex()) && Objects.equals(responsiblePerson.getPersonType(), Constants.PERSON_TYPE_ENGINEERS)) {
                    projectNodeInfo.setDesignEngineers(responsiblePerson.getResponsiblePersonName());
                    projectNodeInfo.setDesignEngineersId(responsiblePerson.getResponsiblePersonId());
                    projectNodeInfo.setDesignCompletionTime(responsiblePerson.getCompleteTime());
                }
            }
        }
        DesignProject designProject = projectMapper.selectById(projectDTO.getProjectId());
        if (Objects.nonNull(designProject.getValorLeader())) {
            projectNodeInfo.setValorEngineersId(designProject.getValorLeader());
            String nickName = userService.queryNickName(designProject.getValorLeader());
            projectNodeInfo.setValorEngineers(nickName);
        }
        BeanUtils.copyProperties(designProject, projectNodeInfo);
        return projectNodeInfo;
    }

    @Override
    @DSTransactional
    public Boolean projectCompleted(DesignProjectDTO projectDTO) {
        //进行中  激活项目
        if (Objects.equals(projectDTO.getUpdateStatus(), "10")) {
            DesignProject project = new DesignProject();
            project.setId(projectDTO.getProjectId());
            project.setProjectStatusAuxiliary(ProjectStatus.IN_PROGRESS.getIndex());
            return projectMapper.updateById(project) > 0;
        }
        //查询有没分配  没有分配不能确认完成
        DesignProject designProject = projectMapper.selectById(projectDTO.getProjectId());
        //暂停  无需封装  这两种情况不需要校验是否分配了负责人
        if ((Objects.isNull(projectDTO.getIsPage()) || projectDTO.getIsPage().equals("1")) && (Objects.isNull(projectDTO.getUpdateStatus()) || !projectDTO.getUpdateStatus().equals("0"))) {
            if ((designProject.getStatus().equals(DesignStatus.ENCAPSULATION.getIndex()) && Objects.isNull(designProject.getPageLeader()))
                    || (designProject.getStatus().equals(DesignStatus.DESIGN.getIndex()) && Objects.isNull(designProject.getDesignLeader()))) {
                throw new MeException("请先分配负责人");
            }
        }
        //查询有没交流信息  没有不能提交
        LambdaQueryWrapper<DesignTalk> eq = Wrappers.lambdaQuery(DesignTalk.class).eq(DesignTalk::getProjectId, projectDTO.getProjectId());
        eq.eq(DesignTalk::getStatus, projectDTO.getStatus());
        List<DesignTalk> talkList = designTalkMapper.selectList(eq);
        //暂停项目不进来  无需封装不进来
        if ((Objects.isNull(projectDTO.getUpdateStatus()) || Objects.equals(projectDTO.getUpdateStatus(), "20"))
                && !Objects.equals(projectDTO.getIsPage(), "0") && CollectionUtil.isEmpty(talkList)) {
            throw new MeException("请先增加至少一条：" + DesignStatus.getName(projectDTO.getStatus()) + "交流表");
        }
        //查询projectId最新一条数据 更新完成时间为当前时间
        DesignResponsiblePerson person = responsiblePersonMapper.selectOne(Wrappers.lambdaQuery(DesignResponsiblePerson.class)
                .eq(DesignResponsiblePerson::getProjectId, projectDTO.getProjectId())
                .orderByDesc(DesignResponsiblePerson::getUpdateTime)
                .last("limit 1"));
        if (person != null) {
            person.setCompleteTime(LocalDateTime.now());
            person.setIsComplete(Constants.YES_COMPLETE);
            responsiblePersonMapper.updateById(person);
        }

        DesignProject project = new DesignProject();
        //暂停
        project.setId(projectDTO.getProjectId());
        if (Objects.equals(projectDTO.getUpdateStatus(), "0")) {
            project.setProjectStatusAuxiliary(ProjectStatus.STOP.getIndex());
            return projectMapper.updateById(project) > 0;
        }
        project.setProjectStatusAuxiliary(ProjectStatus.IN_PROGRESS.getIndex());

        //维护记录
        DesignResponsiblePerson designResponsiblePerson = new DesignResponsiblePerson();
        BeanUtils.copyProperties(projectDTO, designResponsiblePerson);
        designResponsiblePerson.setProjectId(projectDTO.getProjectId());
        designResponsiblePerson.setPersonType(Constants.PERSON_TYPE_COMPETENT);
        String index = DesignStatus.getNextStatusByIndex(Integer.parseInt(projectDTO.getStatus())).getIndex();
        designResponsiblePerson.setNodeStatus(index);

        String nickName = userService.queryNickName(designResponsiblePerson.getResponsiblePersonId());
        designResponsiblePerson.setResponsiblePersonName(nickName);

        //封装完成
        if (projectDTO.getStatus().equals(DesignStatus.ENCAPSULATION.getIndex())) {
            project.setStatus(DesignStatus.DESIGN.getIndex());
            project.setDesignDepartments(projectDTO.getDesignDepartments());
//            project.setValorLeader(projectDTO.getResponsiblePersonId());

            //项目负责人数据调整 设计完成 不需要选择部门和负责人 默认到valor
//            DesignProject oldProject = projectMapper.selectById(projectDTO.getProjectId());
            List<Integer> relevantResponsiblePerson = designProject.getRelevantResponsiblePerson();
            relevantResponsiblePerson.add(Integer.valueOf(projectDTO.getResponsiblePersonId()));
            project.setRelevantResponsiblePerson(relevantResponsiblePerson);
        } else {
            //校验进度是否 100%
            if (Objects.isNull(designProject.getCompletionRate()) || designProject.getCompletionRate() != 100) {
                throw new MeException("项目进度未达：100%，请先更新进度，现在进度为：" + designProject.getCompletionRate() + "%");
            }
            //修改项目状态
            project.setStatus(DesignStatus.CHECK.getIndex());
            project.setCompletionRate(designProject.getCompletionRate());
            //第一次检查时，把项目表的负责人字段数据 复制到待检查负责人字段 用于页面 检查字段做显隐 并且加入valor部门的人
            //获取valor 部门的人
            List<Integer> longs = userMapper.queryByFunction("3");
            List<Integer> relevantResponsiblePerson = designProject.getRelevantResponsiblePerson();
            //塞入valor检查的人
            relevantResponsiblePerson.addAll(longs);
            project.setCheckPersonList(relevantResponsiblePerson);
            //把valor的人加到数据权限里面去
            project.setRelevantResponsiblePerson(relevantResponsiblePerson);
            project.setDesignStatus("");
            //todo  前期测试写死
            project.setValorLeader("00023");
            projectDTO.setValorLeader("00023");
            //todo 维护valor记录 写死
            String valorNickName = userService.queryNickName("00023");
            designResponsiblePerson.setResponsiblePersonName(valorNickName);
            designResponsiblePerson.setResponsiblePersonId("00023");
            //生成检查记录
            this.createCheckList(projectDTO);
        }
        designResponsiblePerson.setDeptId(projectDTO.getDesignDepartments());
        DesignDept designDept = designDeptMapper.selectById(projectDTO.getDesignDepartments());
        designResponsiblePerson.setDeptName(designDept.getDeptName());
        responsiblePersonMapper.insert(designResponsiblePerson);

        project.setResponsiblePersonId(projectDTO.getResponsiblePersonId());
        project.setResponsiblePersonName(userService.queryNickName(projectDTO.getResponsiblePersonId()));

        //修改项目基本信息
        this.updateProjectInfo(projectDTO);

        return projectMapper.updateById(project) > 0;
    }

    public void createCheckList(DesignProjectDTO projectDTO) {
        LambdaQueryWrapper<DesignResponsiblePerson> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DesignResponsiblePerson::getProjectId, projectDTO.getProjectId())
                .eq(DesignResponsiblePerson::getDel, Constants.DEL_FLAG_NO);
        List<DesignResponsiblePerson> responsiblePeople = responsiblePersonMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(responsiblePeople)) {
            throw new MeException("未找到项目节点负责人");
        }
        //封装节点
        List<DesignResponsiblePerson> pageList = responsiblePeople.stream().filter(item -> Objects.equals(item.getNodeStatus(), DesignStatus.ENCAPSULATION.getIndex())).toList();
        if (!CollectionUtil.isEmpty(pageList)){
            //主管
            List<DesignResponsiblePerson> competentList = pageList.stream().filter(item -> Objects.equals(item.getPersonType(), Constants.PERSON_TYPE_COMPETENT))
                    .toList();
            List<DesignResponsiblePerson> engineersList = pageList.stream().filter(item -> Objects.equals(item.getPersonType(), Constants.PERSON_TYPE_ENGINEERS))
                    .toList();
            // 判断两者的 responsiblePersonId 是否相等
            boolean isPersonIdEqual = false;
            if (!competentList.isEmpty() && !engineersList.isEmpty()) {
                isPersonIdEqual = Objects.equals(
                        competentList.get(0).getResponsiblePersonId(),
                        engineersList.get(0).getResponsiblePersonId()
                );
            }
            CheckValue checkValue = new CheckValue();
            checkValue.setVersion(1);
            checkValue.setProjectId(projectDTO.getProjectId());
            checkValue.setNodeName("1");
            checkValue.setCheckedBy(engineersList.get(0).getResponsiblePersonId());
            checkValue.setCheckedName(userService.queryNickName(engineersList.get(0).getResponsiblePersonId()));
            checkValue.setCheckType(1);
            checkValue.setCheckResults(1);
            checkValue.setCreateBy(projectDTO.getCreateBy());
            checkValueMapper.insert(checkValue);
            if (!isPersonIdEqual){
                checkValue.setCheckedBy(competentList.get(0).getResponsiblePersonId());
                checkValue.setCheckedName(userService.queryNickName(competentList.get(0).getResponsiblePersonId()));
                checkValue.setNodeName("2");
                checkValue.setId(null);
                checkValueMapper.insert(checkValue);
            }
        }


        //设计节点
        List<DesignResponsiblePerson> designCompetentList = responsiblePeople.stream().filter(item -> Objects.equals(item.getNodeStatus(), DesignStatus.DESIGN.getIndex())
                        &&  Objects.equals(item.getPersonType(), Constants.PERSON_TYPE_COMPETENT)).toList();
        DesignResponsiblePerson designResponsiblePerson = designCompetentList.get(0);
        String responsiblePersonId = designResponsiblePerson.getResponsiblePersonId();
        //设计主管和负责人不一致 才添加记录
        if (!Objects.equals(responsiblePersonId,projectDTO.getResponsiblePersonId())){
            CheckValue checkValue = new CheckValue();
            checkValue.setVersion(1);
            checkValue.setProjectId(projectDTO.getProjectId());
            checkValue.setNodeName("5");
            checkValue.setCheckedBy(responsiblePersonId);
            checkValue.setCheckedName(userService.queryNickName(responsiblePersonId));
            checkValue.setCheckType(2);
            checkValue.setCheckResults(1);
            checkValueMapper.insert(checkValue);
        }
        //valor节点
        CheckValue checkValue = new CheckValue();
        checkValue.setVersion(1);
        checkValue.setProjectId(projectDTO.getProjectId());
        checkValue.setNodeName("6");
        //todo 测试阶段写死
        checkValue.setCheckedBy(projectDTO.getValorLeader());
        checkValue.setCheckedName(userService.queryNickName(projectDTO.getValorLeader()));
        checkValue.setCheckType(3);
        checkValue.setCheckResults(1);
        checkValueMapper.insert(checkValue);
        //去除checkPersonList设计负责人
        DesignProject designProject = projectMapper.selectById(projectDTO.getProjectId());
        List<Integer> list = designProject.getRelevantResponsiblePerson().stream().filter(item -> !Objects.equals(item, Integer.valueOf(designProject.getResponsiblePersonId()))).toList();
        designProject.setCheckPersonList(list);
        projectMapper.updateById(designProject);
    }

}
